import requests
import pandas as pd

class LightCurveByFITSFetcher:
    def __init__(self, base_url):
        """
        初始化查询器
        :param base_url: Flask 后端接口主地址，如 http://192.168.16.70:8082
        """
        self.base_url = base_url.rstrip('/')  # 去除末尾斜杠避免拼接错误

    def fetch(self, fitsname):
        """
        查询指定 fitsname 的光变数据
        :param fitsname: 完整的 fits 文件名（例如 y50a_12377_-0012mr__sciimg_sexcat_CalMag.fits）
        :param band: 波段名称 ('r', 'g', 'i' 或 'all')
        :return: pandas.DataFrame 结果
        """
        endpoint = f"{self.base_url}/api/query_lc_byfits"
        params = {
            'fitsname': fitsname
        }

        response = requests.get(endpoint, params=params)
        if response.status_code != 200:
            raise RuntimeError(f"❌ 请求失败: {response.status_code}\n{response.text}")

        return pd.DataFrame(response.json())

# 主程序入口
if __name__ == "__main__":
    # 配置参数
    BASE_URL = "http://192.168.16.70:8082/V20250303_KM"
    FITSNAME = "mr_sc_t1278305-19695_i_20250122154303_032_sciimg_sexcat_CalMag.fits"
    BAND = "all"  # 或 "r", "g", "i"

    # 实例化并查询
    fetcher = LightCurveByFITSFetcher(BASE_URL)
    df = fetcher.fetch(FITSNAME)

    # 打印结果
    print("✅ 查询结果：")
    print(df.head())
